<template>
  <div class="book-detail">
    <div class="page-header">
      <el-button @click="$router.back()" class="back-btn">
        <el-icon><ArrowLeft /></el-icon>
        返回
      </el-button>
      <h1>图书详情</h1>
    </div>

    <el-card v-loading="loading">
      <div v-if="book" class="book-info">
        <div class="book-main">
          <div class="book-cover-section">
            <el-image
              :src="book.coverUrl || '/default-book-cover.jpg'"
              :alt="book.name"
              class="book-cover"
              fit="cover"
            >
              <template #error>
                <div class="cover-placeholder">
                  <el-icon><Picture /></el-icon>
                </div>
              </template>
            </el-image>
            
            <div class="book-actions">
              <el-button
                type="primary"
                size="large"
                :disabled="book.status !== '0' || book.available <= 0"
                @click="handleBorrow"
              >
                借阅图书
              </el-button>
            </div>
          </div>
          
          <div class="book-details">
            <h1>{{ book.name }}</h1>
            <div class="book-meta">
              <p><strong>作者：</strong>{{ book.author }}</p>
              <p><strong>ISBN：</strong>{{ book.isbn }}</p>
              <p><strong>出版社：</strong>{{ book.publisher }}</p>
              <p><strong>出版日期：</strong>{{ book.publishDate }}</p>
              <p><strong>分类：</strong>
                <el-tag size="small">{{ book.categoryName }}</el-tag>
              </p>
              <p><strong>状态：</strong>
                <el-tag :type="getStatusType(book.status)" size="small">
                  {{ getStatusText(book.status) }}
                </el-tag>
              </p>
              <p><strong>库存：</strong>
                              <span :class="{ 'low-stock': book.total <= 5 }">
                {{ book.available }}/{{ book.total }}
                </span>
              </p>
            </div>
            
            <div class="book-description">
              <h3>内容简介</h3>
              <p>{{ book.description || '暂无描述' }}</p>
            </div>
          </div>
        </div>
      </div>
    </el-card>

    <!-- 借阅对话框 -->
    <BorrowDialog
      v-model:visible="borrowDialogVisible"
      :book="book"
      @success="handleBorrowSuccess"
    />
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import request from '@/utils/request'
import BorrowDialog from '@/components/BorrowDialog.vue'

const route = useRoute()
const router = useRouter()

const loading = ref(false)
const book = ref(null)
const borrowDialogVisible = ref(false)

const getStatusType = (status) => {
  const statusMap = {
    'AVAILABLE': 'success',
    'BORROWED': 'warning',
    'MAINTENANCE': 'danger'
  }
  return statusMap[status] || 'info'
}

const getStatusText = (status) => {
  const statusMap = {
    'AVAILABLE': '可借阅',
    'BORROWED': '已借出',
    'MAINTENANCE': '维护中'
  }
  return statusMap[status] || '未知'
}

const loadBook = async () => {
  try {
    loading.value = true
    const response = await request.get(`/books/${route.params.id}`)
    book.value = response.data
  } catch (error) {
    console.error('加载图书详情失败:', error)
    ElMessage.error('加载图书详情失败')
  } finally {
    loading.value = false
  }
}

const handleBorrow = () => {
  borrowDialogVisible.value = true
}

const handleBorrowSuccess = () => {
  loadBook()
}

onMounted(() => {
  loadBook()
})
</script>

<style scoped>
.book-detail {
  padding: 0;
}

.page-header {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 24px;
}

.back-btn {
  padding: 8px 16px;
}

.page-header h1 {
  margin: 0;
  font-size: 24px;
  color: #333;
}

.book-info {
  padding: 20px;
}

.book-main {
  display: flex;
  gap: 40px;
}

.book-cover-section {
  flex-shrink: 0;
  text-align: center;
}

.book-cover {
  width: 200px;
  height: 280px;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.cover-placeholder {
  width: 200px;
  height: 280px;
  background: #f5f5f5;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #ccc;
  font-size: 48px;
}

.book-actions {
  margin-top: 20px;
}

.book-details {
  flex: 1;
}

.book-details h1 {
  font-size: 28px;
  color: #333;
  margin: 0 0 20px 0;
  font-weight: 600;
}

.book-meta {
  margin-bottom: 30px;
}

.book-meta p {
  margin: 8px 0;
  font-size: 16px;
  color: #666;
  line-height: 1.6;
}

.book-meta strong {
  color: #333;
  font-weight: 600;
  display: inline-block;
  width: 80px;
}

.low-stock {
  color: #e6a23c;
  font-weight: bold;
}

.book-description {
  border-top: 1px solid #f0f0f0;
  padding-top: 20px;
}

.book-description h3 {
  font-size: 18px;
  color: #333;
  margin: 0 0 12px 0;
  font-weight: 600;
}

.book-description p {
  font-size: 14px;
  color: #666;
  line-height: 1.8;
  text-align: justify;
}

@media (max-width: 768px) {
  .book-main {
    flex-direction: column;
    gap: 20px;
  }
  
  .book-cover {
    width: 150px;
    height: 210px;
  }
  
  .cover-placeholder {
    width: 150px;
    height: 210px;
  }
}
</style> 